//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "ShortRateModel.h"
using namespace Cephei::QL::Models;
#include <gen/QL/Lattice.h>
#include <gen/QL/TimeGrid.h>
#include <gen/QL/Models/CalibrationHelper.h>
#include <gen/QL/Math/Optimization/OptimizationMethod.h>
#include <gen/QL/Math/Optimization/EndCriteria.h>
#include <gen/QL/Math/Optimization/Constraint.h>
#include <gen/QL/Math/Array.h>
#include <gen/QL/Models/CalibratedModel.h>
using namespace Cephei::QL;
using namespace Cephei::QL::Math::Optimization;
using namespace Cephei::QL::Math;
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Models::CShortRateModel::CShortRateModel (boost::shared_ptr<QuantLib::ShortRateModel>& childNative, Object^ owner) : CCalibratedModel(CShortRateModel::typeid)
{
#ifdef HANDLE
	_phShortRateModel = NULL;
#endif
	_ppShortRateModel = &childNative;
    _ppCalibratedModel = new boost::shared_ptr<QuantLib::CalibratedModel> (boost::dynamic_pointer_cast<QuantLib::CalibratedModel> (*_ppShortRateModel));
}
Cephei::QL::Models::CShortRateModel::CShortRateModel (QuantLib::ShortRateModel& childNative, Object^ owner) : CCalibratedModel(CShortRateModel::typeid)
{
#ifdef HANDLE
	_phShortRateModel = NULL;
#endif
	_ppShortRateModel = new boost::shared_ptr<QuantLib::ShortRateModel> (&childNative);
    _ppCalibratedModel = new boost::shared_ptr<QuantLib::CalibratedModel> (boost::dynamic_pointer_cast<QuantLib::CalibratedModel> (*_ppShortRateModel));
    _ShortRateModelOwner = owner;
    _CalibratedModelOwner = owner;
}

Cephei::QL::Models::CShortRateModel::CShortRateModel (CShortRateModel^ copy) : CCalibratedModel(CShortRateModel::typeid)
{
#ifdef HANDLE
	_phShortRateModel = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppShortRateModel = new boost::shared_ptr<QuantLib::ShortRateModel> (copy->GetShared());
        _ppCalibratedModel = new boost::shared_ptr<QuantLib::CalibratedModel> (boost::dynamic_pointer_cast<QuantLib::CalibratedModel> (*_ppShortRateModel));
    }
}
Cephei::QL::Models::CShortRateModel::CShortRateModel (System::Type^ t) : CCalibratedModel(CShortRateModel::typeid)
{
#ifdef HANDLE
	_phShortRateModel = NULL;
#endif
	if (!t->IsSubclassOf(CShortRateModel::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::CShortRateModel::CShortRateModel (QuantLib::Handle<QuantLib::ShortRateModel>& childNative, Object^ owner)  : CCalibratedModel(CShortRateModel::typeid)
{
	_phShortRateModel = &childNative;
	_ppShortRateModel = &static_cast<boost::shared_ptr<QuantLib::ShortRateModel>>(childNative.currentLink());
    _ppCalibratedModel = new boost::shared_ptr<QuantLib::CalibratedModel> (boost::dynamic_pointer_cast<QuantLib::CalibratedModel> (*_ppShortRateModel));
    _ShortRateModelOwner = owner;
}
Cephei::QL::Models::CShortRateModel::CShortRateModel (QuantLib::Handle<QuantLib::ShortRateModel> childNative)  : CCalibratedModel(CShortRateModel::typeid)
{
	_phShortRateModel = &childNative;
	_ppShortRateModel = &static_cast<boost::shared_ptr<QuantLib::ShortRateModel>>(childNative.currentLink());
    _ppCalibratedModel = new boost::shared_ptr<QuantLib::CalibratedModel> (boost::dynamic_pointer_cast<QuantLib::CalibratedModel> (*_ppShortRateModel));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::CShortRateModel::CShortRateModel (QuantLib::ShortRateModel childNative)  : CCalibratedModel(CShortRateModel::typeid)
{
#ifdef HANDLE
	_phShortRateModel = NULL;
#endif
	_ppShortRateModel = new boost::shared_ptr<QuantLib::ShortRateModel> (new QuantLib::ShortRateModel (childNative));
    _ppCalibratedModel = new boost::shared_ptr<QuantLib::CalibratedModel> (boost::dynamic_pointer_cast<QuantLib::CalibratedModel> (*_ppShortRateModel));
}
#endif

Cephei::QL::Models::CShortRateModel::~CShortRateModel ()
{
    if (_ppShortRateModel != NULL)
    {
	    delete _ppShortRateModel;
        _ppShortRateModel = NULL;
    }
}
Cephei::QL::Models::CShortRateModel::!CShortRateModel ()
{
    if (_ppShortRateModel != NULL)
    {
	    delete _ppShortRateModel;
    }
}
QuantLib::ShortRateModel& Cephei::QL::Models::CShortRateModel::GetReference ()
{
    if (_ppShortRateModel == NULL) throw gcnew NativeNullException ();
	return **_ppShortRateModel;
}
boost::shared_ptr<QuantLib::ShortRateModel>& Cephei::QL::Models::CShortRateModel::GetShared ()
{
    if (_ppShortRateModel == NULL) throw gcnew NativeNullException ();
	return *_ppShortRateModel;
}
QuantLib::ShortRateModel* Cephei::QL::Models::CShortRateModel::GetPointer ()
{
    if (_ppShortRateModel == NULL) throw gcnew NativeNullException ();
	return &**_ppShortRateModel;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ShortRateModel>& Cephei::QL::Models::CShortRateModel::GetHandle ()
{
	if (_phShortRateModel == NULL)
	{
		_phShortRateModel = new Handle<QuantLib::ShortRateModel> (*_ppShortRateModel);
	}
	return *_phShortRateModel;
}
#endif
bool Cephei::QL::Models::CShortRateModel::HasNative () 
{
	return (_ppShortRateModel != NULL);
}

Cephei::QL::ILattice^ Cephei::QL::Models::CShortRateModel::Tree (Cephei::QL::ITimeGrid^ prm1)
{
    CTimeGrid^ _Cprm1;
    try
    {
        _Cprm1 = safe_cast<CTimeGrid^> (prm1);
        _Cprm1->Lock();
        QuantLib::TimeGrid& _prm1 = static_cast<QuantLib::TimeGrid&> (_Cprm1->GetReference ()); 
    	boost::shared_ptr<QuantLib::Lattice> _rv = (boost::shared_ptr<QuantLib::Lattice>)(*_ppShortRateModel)->tree ( _prm1 );   
        Cephei::QL::CLattice^ _nrv = gcnew Cephei::QL::CLattice (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprm1 != nullptr) _Cprm1->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

